package com.waiting.serializable;

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/2/14
 **/
public class PersonProxy implements Serializable {

    public PersonProxy(Person orig)
    {
        data = orig.getFirstName() + "," + orig.getLastName() + "," + orig.getAge();
        if (orig.getSpouse() != null)
        {
            Person spouse = orig.getSpouse();
            data = data + "," + spouse.getFirstName() + "," + spouse.getLastName() + ","
                    + spouse.getAge();
        }
    }

    public String data;

    private Object readResolve() throws ObjectStreamException
    {
        String[] pieces = data.split(",");
        Person result = new Person(pieces[0], pieces[1], Integer.parseInt(pieces[2]));
        if (pieces.length > 3)
        {
            result.setSpouse(new Person(pieces[3], pieces[4], Integer.parseInt
                    (pieces[5])));
            result.getSpouse().setSpouse(result);
        }
        return result;
    }
}